<?php
namespace app\dlc\controller;
use app\common\model\Form;
use app\common\model\FormItem;
use app\common\model\Site;
use think\Db;

class FormItemController extends CommonController
{

    public function _initialize(){
        parent::_initialize();

    }
    public function index()
    {
        $this->title = '表字段列表';
        if ($this->map['form_id']){
            $this->headBotton('','back','返回','/'.MODULE_NAME.'/form/index');
        }
        $this->headBotton('','add','添加','/'.MODULE_NAME.'/'.$this->className.'/add_ajax/form_id/'.input('form_id'),'2');
        $this->headSearch('','name','字段名称','1');
        $this->headSearch('','form_id','所属识别名',2,Db::name('form')->column('id,index_name'));
        $this->listBotton('','edit','编辑','/'.MODULE_NAME.'/'.$this->className.'/_edit_ajax',2,'','','','',['onClick' => 'openNewWindows']);
        $this->listBotton('','device','定义规则','/'.MODULE_NAME.'/'.$this->className.'/rule',2,'','','','',['onClick' => 'openNewWindows']);
        $this->listBotton('','delete','删除','/'.MODULE_NAME.'/'.$this->className.'/_del');
        $this->loading();
        $map = $this->map;
        $data = model($this->className)->where($map)->order('sort asc')->page(self::$page)->select();
        foreach ($data as $key => $value) {
            $data[$key]['form_name'] = $data[$key]['form_name']?:$value['form']['name'];
            $data[$key]['index_name'] = $value['form']['index_name'];
            $depend = FormItem::get($value['depend']);
            $data[$key]['depend'] = $value['depend']?$depend['key'].$value['depend_condition'].$value['depend_value']:'/';
            $data[$key]['sort'] = $this->listInput('',$value['id'],'sort',$value['sort']);
            $data[$key]['title'] = $this->listInput('',$value['id'],'title',$value['title']);
        }
        $count = Db::name($this->className)->where($map)->count();
        $this->getPage($count, self::$psize, 'App-loader', $this->title, 'App-search');
        $this->assign('data',$data);
        $list = ['id'=>'ID','key'=>'字段名','form_name'=>'所属表','index_name'=>'识别名','title'=>'中文名','typeText'=>'类型','rule'=>'验证规则','depend'=>'依赖','sort'=>'排序（从小到大）','ctime'=>'创建时间','sys'=>'操作'];
        $this->assign('list',$list);
        echo $this->fetch('common/index');
    }

    public function edit(){
        if ($_POST){
            $post = input('post.');
            $form = Form::get($post['form_id']);
            $formData = \db()->getTableInfo('dlc_'.$form['name'],'fields');
            if (!in_array($post['key'],$formData)) {admin_return(0,'该字段不存在');}
//            if (!isset($formData[$post['key']])) admin_return(0,'该字段不存在');
        }

        $this->_edit();
        echo $this->fetch('common/edit');
    }

    //添加时 弹窗
    public function add_ajax(){
        $post = input('post.');
        $this->form = 'form_item_add';
        $form_id = input('form_id');
        if (count($post)>1){
            $res = Db::name('form_item')->where(['form_id'=>$form_id,'key'=>$post['key']])->find();
            if ($res) admin_return(0,'字段名已存在');
        }
        $form = Form::get($form_id);
        $formData = \db()->getTableInfo('dlc_'.$form['name'],'fields');
        $key_name = self::get_db_column_comment($form['name']);
        $data = [];
        foreach ($formData as $key=>$value) {
            $data[$value] = $value.'|'.$key_name[$value];
        }
        $this->formAddArray['key']['data'] = $data;
        $add['form_id'] = $form_id;
        $add['ctime'] = time();
        $this->_edit_ajax('',$add);
    }

    public function rule(){
        $this->form = 'form_rule';
        $post = input('post.');
        if (count($post)>1){
            if ($post['data']){
                $a = json_decode($post['data'],true);
                if (!is_array($a))admin_return(0,'data数组格式不对');
            }
            if ($post['data_map']){
                $a = json_decode($post['data_map'],true);
                if (!is_array($a))admin_return(0,'data_map数组格式不对');
            }
        }
        $id = input('id');
        $formItem = FormItem::get($id);
        $data = Db::name($this->className)->where(['form_id'=>$formItem['form_id']])->column('id,key');
        unset($data[$formItem['id']]);
        $add['depend']['data'] = $data;
        $this->formAddArray = $add;
        $this->_edit_ajax();
    }







}